package wangjg.xuexiban.activity.card;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.List;

import wangjg.commons.io.file.FileIOHelper;
import wangjg.xuexiban.R;
import wangjg.xuexiban.activity.BasicBizActivity;
import wangjg.xuexiban.entity.SimpleCard;
import wangjg.xuexiban.util.Constant;
import wangjg.xuexiban.util.FileDownCallback;
import wangjg.xuexiban.util.FileDownCallback.DownloadVo;
import wangjg.xuexiban.util.JsonUtil;
import wangjg.xuexiban.vo.JsonResponse;
import wangjg.xuexiban.vo.PageData;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public abstract class BasicCardActivity extends BasicBizActivity {
	private MediaPlayer mediaPlayer = new MediaPlayer();
	private List<SimpleCard> cardList;
	protected int currentIndex = 0;
	
	protected View createPageView(int position){
		final View itemView = this.inflate(R.layout.card_item);
		final SimpleCard card = this.getCardList().get(position);
		String name = card.getName();
		String speechUrl = card.getSpeech();
		String picPath = card.getPicture();
		TextView tv = (TextView) itemView.findViewById(R.id.name);
		tv.setText(name);
		
		this.download(Constant.fileUrl(speechUrl), FileDownCallback.TYPE_AUDIO, itemView.findViewById(R.id.imageView_play));
		this.download(Constant.picUrl(picPath), FileDownCallback.TYPE_IMG, itemView.findViewById(R.id.imageView1));
		return itemView;
	}
	
    protected void handleData(DownloadVo dv) {
		String json = FileIOHelper.FileToString(dv.filePath, "utf-8");
		JsonResponse response = JsonUtil.getCardListResp(json);
		if(response.noError()){
			PageData<SimpleCard> pageData = (PageData<SimpleCard>)response.getData();
			cardList = pageData.getList();
			initContentView();
		}else{
			Toast.makeText(BasicCardActivity.this, "数据错误:"+response.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}
    
    protected void handleAudio(DownloadVo dv){
    	setAudio((ImageView)dv.obj, dv.filePath);
    }

    protected void handleImage(DownloadVo dv){
		ImageView iv = (ImageView) dv.obj;
		setImage(iv, dv.filePath);
    }
 
    protected void initContentView(){
    	
    }
    
    protected void setImage(ImageView iv, String filePath){
    	Bitmap bitmap = rc.get(filePath);
		iv.setImageBitmap(bitmap);
    }
    
    protected void setAudio(ImageView iv, final String filePath){
		final ImageView btn_play = (ImageView) iv;
		btn_play.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				if(mediaPlayer.isPlaying()){
					stopVoice();
					setPlayIcon(btn_play);
				}else{
					playVoice(filePath, new PlayListener(btn_play));
					setStopIcon(btn_play);
				}
			}
	    });
    }
	
	public List<SimpleCard> getCardList() {
		if(cardList==null){
			loadDataList();
		}
		return cardList;
	}
	
	protected void loadDataList() {
		String categoryId = getIntent().getStringExtra("category_path");
		String url = Constant.URL_CARD_DOWN + categoryId;
		
		this.download(url, FileDownCallback.TYPE_DATA, null);
	}
	
	int ______________;
	
	protected void onStop() {
		super.onStop();
		this.stopVoice();
	}
	
    protected void onDestroy() {
    	super.onDestroy();
    	this.mediaPlayer.release();
    }

	private void playVoice(String url, PlayListener pl){
		Log.d("play ", ""+url);
		if(mediaPlayer.isPlaying()){
			return;
		}
		
		mediaPlayer.reset();
		mediaPlayer.setOnCompletionListener(pl);
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		try {
			if(url!=null && new File(url).exists()){
				mediaPlayer.setDataSource(url);	
			}else{
				AssetManager assetManager = getAssets();
				AssetFileDescriptor afd = assetManager.openFd("sound/novoice26.wav");
				FileDescriptor fd = afd.getFileDescriptor();
				mediaPlayer.setDataSource(fd, afd.getStartOffset(), afd.getLength());
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		try {
			mediaPlayer.prepare();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mediaPlayer.start();
	
	}

	private void stopVoice(){
		if(mediaPlayer.isPlaying()){
			mediaPlayer.stop();
		}
	}

	private void setStopIcon(ImageView btn_play){
		btn_play.setImageResource(R.drawable.stop);
		Log.d("setStopIcon","setStopIcon");
    }
    
    private void setPlayIcon(ImageView btn_play){
		btn_play.setImageResource(R.drawable.play);
		Log.d("setPlayIcon","setPlayIcon");
    }
    
	class PlayListener implements OnCompletionListener ,OnErrorListener {
		private ImageView btn_play;
		
		PlayListener(ImageView btn_play){
			this.btn_play = btn_play;
		}
		public void onCompletion(MediaPlayer mp) {
			setPlayIcon(btn_play);
		}
	
		public boolean onError(MediaPlayer mp, int what, int extra) {
			
			return false;
		}
	}
	
	public static class ItemSelectedListener implements OnItemSelectedListener{

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
		}

		public void onNothingSelected(AdapterView<?> parent) {
			
		}
	}
}
